$(function(){
	dictionaryStart("model","141001");
	dictionaryStart("productionfactory","141002");
	dictionaryStart("countertype","141003");
	dictionaryStart("counterfactory","141004");
	countertypeStart();
	counterfactoryStart();
	$("#combinationdevicetype").bind('change',function(){
		zhsbChange();
	});
});
function zhsbChange(){
	var type=$("#combinationdevicetype").val();
	if(type=="否"){
		$("input[name='combinationdevicename']").val("");
		$("input[name='combinationdevicename']").attr("readonly","readonly");
	}else{
		$("input[name='combinationdevicename']").removeAttr("readonly");
	}
}

var countertypeStart=function(){
	$.ajax({
		url : contextPath+"user/dictionary/list",
		data : {
			typeId : '141003'
		},
		type : 'POST',
		async : false,
		success:function(list){
			if(null!=list && list.length>0){
				$("#countertype").empty().show();
				var option="";
				var bl=false;
				for(var i=0;i<list.length;i++){
					if(countertype && list[i].dictionaryValue==countertype){
						bl=true;
					}
					option += '<option value="'+list[i].dictionaryValue+'">'+list[i].dictionaryValue+'</option>';
				}
				if(!bl){
					option += '<option value="'+countertype+'">'+countertype+'</option>';
				}
				$('#countertype').append(option);
				$('#countertype').val(countertype);
			}
			
		}
	});
};

var counterfactoryStart=function(){
	$.ajax({
		url : contextPath+"user/dictionary/list",
		data : {
			typeId : '141004'
		},
		type : 'POST',
		async : false,
		success:function(list){
			if(null!=list && list.length>0){
				$("#counterfactory").empty().show();
				var option="";
				for(var i=0;i<list.length;i++){
					option += '<option value="'+list[i].dictionaryValue+'">'+list[i].dictionaryValue+'</option>';
				}
				$('#counterfactory').append(option);
				
				$("#counterfactory").empty().show();
				var option="";
				var bl=false;
				for(var i=0;i<list.length;i++){
					if(counterfactory && list[i].dictionaryValue==counterfactory){
						bl=true;
					}
					option += '<option value="'+list[i].dictionaryValue+'">'+list[i].dictionaryValue+'</option>';
				}
				if(!bl){
					option += '<option value="'+counterfactory+'">'+counterfactory+'</option>';
				}
				$('#counterfactory').append(option);
				$('#counterfactory').val(counterfactory);
			}
		}
	});
};

function add(){
	var devicename=$("#devicename").val();
	if(devicename==""){
		alert('设备名称为空');
		$("#devicename").focus();
		return;
	}
	/*if($("input[name='localcity']").val()==""){
		alert("所属地市为空");
		$("input[name='localcity']").focus();
		return;
	}
	if($("input[name='operationunit']").val()==""){
		alert("运维单位为空");
		$("input[name='operationunit']").focus();
		return;
	}
	if($("input[name='maintenanceteam']").val()==""){
		alert("维护单位为空");
		$("input[name='maintenanceteam']").focus();
		return;
	}
	if($("input[name='gapcode']").val()==""){
		alert("间隔单元为空");
		$("input[name='gapcode']").focus();
		return;
	}
	if($("input[name='commissioningdate']").val()==""){
		alert("投运日期为空");
//		$("input[name='commissioningdate']").focus();
		return;
	}
	if($("input[name='equipmentowner']").val()==""){
		alert("设备主人为空");
		$("input[name='equipmentowner']").focus();
		return;
	}
	if($("input[name='ratedvoltage']").val()!=""&&!isVoltage($("input[name='ratedvoltage']").val())){
		alert("额定电压不正确，如220v或1kv");
		return;
	}
	if($("input[name='workingvoltage']").val()!=""&&!isInt($("input[name='workingvoltage']").val())){
		alert("工频放电电压必须为数字");
		$("input[name='workingvoltage']").focus();
		return;
	}
	if($("input[name='assetnumber']").val()==""){
		alert("资产编号为空");
		$("input[name='assetnumber']").focus();
		return;
	}*/
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/zyznblq/add",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				location.href=contextPath+"user/z/zyznblqlist.jsp?deviceid="+deviceid+"&devicetype="+devicetype+"&t="+new Date().getTime();
			}
		}
	});
}

function edite(){
	var devicename=$("#devicename").val();
	if(devicename==""){
		alert('设备名称为空');
		$("#devicename").focus();
		return;
	}
	/*if($("input[name='localcity']").val()==""){
		alert("所属地市为空");
		$("input[name='localcity']").focus();
		return;
	}
	if($("input[name='operationunit']").val()==""){
		alert("运维单位为空");
		$("input[name='operationunit']").focus();
		return;
	}
	if($("input[name='maintenanceteam']").val()==""){
		alert("维护单位为空");
		$("input[name='maintenanceteam']").focus();
		return;
	}
	if($("input[name='gapcode']").val()==""){
		alert("间隔单元为空");
		$("input[name='gapcode']").focus();
		return;
	}
	if($("input[name='commissioningdate']").val()==""){
		alert("投运日期为空");
//		$("input[name='commissioningdate']").focus();
		return;
	}
	if($("input[name='equipmentowner']").val()==""){
		alert("设备主人为空");
		$("input[name='equipmentowner']").focus();
		return;
	}
	if($("input[name='ratedvoltage']").val()!="" && !isVoltage($("input[name='ratedvoltage']").val())){
		alert("额定电压不正确，如220v或1kv");
		return;
	}
	if($("input[name='workingvoltage']").val()!=""&&!isInt($("input[name='workingvoltage']").val())){
		alert("工频放电电压必须为数字");
		$("input[name='workingvoltage']").focus();
		return;
	}
	if($("input[name='assetnumber']").val()==""){
		alert("资产编号为空");
		$("input[name='assetnumber']").focus();
		return;
	}*/
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/zyznblq/edite",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				location.href=contextPath+"user/z/zyznblqlist.jsp?deviceid="+deviceid+"&devicetype="+devicetype+"&t="+new Date().getTime();
			}
		}
	});
}

function setDefault(elementId,data){
	$('#'+elementId).val(data);
}